//
//  MPFacoritiesTableViewController.m
//  SearchPeople
//
//  Created by jcc on 16/5/5.
//
//

#import "MPFavoritiesTableViewController.h"
#import "MPFavoritiesTableViewCell.h"
#import "MPImport.h"
#import "MPSettingsManager.h"
#import "MPClueViewController.h"
#import "MPDocumentHttpClient.h"

@interface MPFavoritiesTableViewController()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *favoritiesTableView;
@property (strong, nonatomic) NSMutableArray *dataSoureArray;
@property (nonatomic, getter=isLogin) BOOL login;

@end
@implementation MPFavoritiesTableViewController

#pragma mark - UITableView Life Cycle
- (void)viewDidLoad{
    self.favoritiesTableView.delegate = self;
    self.favoritiesTableView.dataSource = self;
    self.favoritiesTableView.tableFooterView = [UIView new];
    self.title = @"收藏";
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.login = [[MPSettingsManager getInstance].userDefault objectForKey:@"user"] ? YES : NO;
    if (self.login) {
        [self getDataSoure];
    }else{
        [SVProgressHUD showInfoWithStatus:@"请登录"];
    }
}

- (void)getDataSoure{

    NSDictionary *userDic = [[MPSettingsManager getInstance].userDefault objectForKey:@"user"];
    NSString *user_id = [userDic objectForKey:@"user_id"];
    [[MPDocumentHttpClient shareInstance] GetCollectWithUserID:user_id WithSuccessBlock:^(MPRequest *request, MPResponse *response) {
        self.dataSoureArray = response.result[@"data"];
        [self.favoritiesTableView reloadData];
    } WithFailedBlock:^(MPRequest *request, MPResponse *response) {
        
    }];
}

#pragma mark - UITableView DataSoure
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataSoureArray.count > 0) {
        return self.dataSoureArray.count;
    }else{
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MPFavoritiesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPFavoritiesTableViewCell"];
     [cell ConfigWithDic:self.dataSoureArray[indexPath.row]];
    return cell;
}

#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"HomePage" bundle:nil];
    MPClueViewController *targerVC = [story instantiateViewControllerWithIdentifier:@"MPClueViewController"];
    targerVC.dic = self.dataSoureArray[indexPath.row];
    [self.navigationController pushViewController:targerVC animated:YES];

}

- (NSMutableArray *)dataSoureArray{
    if (!_dataSoureArray) {
        _dataSoureArray = [[NSMutableArray alloc] init];
    }
    return _dataSoureArray;
}
@end
